package com.xhb.ui.collect;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.noear.solon.Solon;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.teamdev.jxbrowser.deps.com.google.common.collect.Lists;
import com.teamdev.jxbrowser.navigation.event.FrameLoadFinished;
import com.xhb.biz.po.CollectUrlPO;
import com.xhb.biz.po.CollectUrlPO.FrameLoadFinishedCall;
import com.xhb.biz.po.ProjectPO;
import com.xhb.biz.service.CollectGroupService;
import com.xhb.biz.service.ProjectService;
import com.xhb.ui.ToolBar;
import com.xhb.ui.resources.Resources;
import com.xhb.ui.tools.UItools;

/**
 * 收藏夹
 */
public class CollectBar extends JPanel {

    private CollectBar collectBar;

    private JPanel left;

    public CollectBar() {
        collectBar = this;
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        setVisible(true);
        add(createCollectPanel(), BorderLayout.CENTER);
//        add(createMoreButton(), BorderLayout.EAST);
    }

    public JPanel createCollectPanel() {
        left = new JPanel();
        left.setLayout(new FlowLayout(FlowLayout.LEFT));
        left.setBackground(Color.WHITE);
        return left;
    }

    /**
     * 刷新收藏夹内容
     */
    public void refresh() {
        setVisible(false);
        left.removeAll();

        //加载项目
        ProjectService projectService = Solon.context().getBean(ProjectService.class);
        List<ProjectPO> projectPOs = projectService.list();
//        CollectGroupButton groupButton;
//        for (ProjectPO projectPO : projectPOs) {
//            groupButton = new CollectGroupButton(true, null, this, null);
//            groupButton.setProjectId(projectPO.getId());
//            groupButton.setText(projectPO.getName());
//            left.add(groupButton);
//        }

        //加载收藏夹文件夹
//        CollectGroupService collectGroupService = Solon.context().getBean(CollectGroupService.class);
//        List<CollectGroupPO> collectGroupPOS = collectGroupService.list(new LambdaQueryWrapper<CollectGroupPO>().orderByAsc(CollectGroupPO::getSortNo));
//        for (CollectGroupPO collectGroupPO : collectGroupPOS) {
//            groupButton = new CollectGroupButton(false, collectGroupPO.getName(), this, collectGroupPO.getId());
//            groupButton.setText(collectGroupPO.getName());
//            left.add(groupButton);
//        }

        //加载单个url
//        CollectUrlService collectUrlService = Solon.context().getBean(CollectUrlService.class);
//        List<CollectUrlPO> urlPOS = collectUrlService.list(new LambdaQueryWrapper<CollectUrlPO>()
//                .isNull(CollectUrlPO::getCollectGroupId).orderByAsc(CollectUrlPO::getSortNo));
        
        List<CollectUrlPO> urlPOS=Lists.newArrayList();
        urlPOS.add(new CollectUrlPO(null, "图片软件下载", null, null, "https://download2.huduntech.com/application/workspace/54/5420801cd2db22fe96ddc91ac26ccb46/Installer_%E5%9B%BE%E7%89%87%E7%BC%96%E8%BE%91%E5%8A%A9%E6%89%8B_r1.7.7.exe", null, null,null));
        urlPOS.add(new CollectUrlPO(null, "图片换衣网站", null, null, "https://cc.aonion.cn/tool/replace", null, null,null));
        urlPOS.add(new CollectUrlPO(null, "江西刷课", null, null, "http://static-mp-0486c7fc-822a-4184-8eeb-614fc1288d46.next.bspapp.com/#/", null, null,null));
        urlPOS.add(new CollectUrlPO(null, "浙江刷课", null, null, "http://static-mp-0a2340ef-6139-43d4-bd3c-b74789381b05.next.bspapp.com/#/", null, null,null));
		urlPOS.add(new CollectUrlPO(null, "重启1号网", null, null,
				"http://zkyd.sharemao.cn/device/getDevInfo.do?sn=865054056442065",
				null, null,new FrameLoadFinishedCall() {
					@Override
					public void doEvent(FrameLoadFinished event) {
						if (event.frame().browser().url().contains("getDevInfo.do")) {
							//调用重启接口
							event.navigation().loadUrl("http://zkyd.sharemao.cn/device/devset.do?type=1");
						}
					}}));
		urlPOS.add(new CollectUrlPO(null, "生成背景图", null, null, "ImgGen", null, null,null));
		urlPOS.add(new CollectUrlPO(null, "江西考试", null, null, "https://zyk.etledu.com/", null, null,null));
		urlPOS.add(new CollectUrlPO(null, "自动答题", null, null, "examjx", null, null,null));
        urlPOS.add(new CollectUrlPO(null, "打开1号云手机", null, null, "http://lebo.kkyp.vip/jbplebo/templates/webrtc-rgc3.html?1=1&instanceId=cp-bp1c9dtvmqyfuyrobws2&ip=47.97.123.208", null, null,null));
    	urlPOS.add(new CollectUrlPO(null, "我的IP", null, null, "https://www.ip138.com/", null, null,null));
       
        CollectUrlButton collectUrlButton;
        for (CollectUrlPO urlPO : urlPOS) {
            collectUrlButton = new CollectUrlButton(urlPO, this);
            collectUrlButton.setText(urlPO.getName());
            int length = urlPO.getName().length();
            if (length > 12) {
                collectUrlButton.setPreferredSize(new Dimension(120, 14));
            }
            String icon = urlPO.getIcon();
            if (StringUtils.isBlank(icon)) {
                collectUrlButton.setIcon(Resources.getIcon("/collect/browser.png"));
            } else {
                ImageIcon imageIcon = UItools.base64ToImageIcon(icon);
                if (null != UItools.base64ToImageIcon(icon)) {
                    collectUrlButton.setIcon(imageIcon);
                } else {
                    collectUrlButton.setIcon(Resources.getIcon("/collect/browser.png"));
                }
            }
            left.add(collectUrlButton);
        }
        setVisible(true);
    }

    public JButton createMoreButton() {
        JButton jButton = new JButton();
        jButton.setIcon(Resources.getIcon("collect/more.png"));
        jButton.add(createMenuButton(), BorderLayout.NORTH);
        UItools.parseButton(jButton);
        return jButton;
    }

    private JComponent createMenuButton() {
        final JPopupMenu popupMenu = new JPopupMenu();

        final ToolBar.ActionButton button = new ToolBar.ActionButton("Preferences", null);
        button.setIcon(Resources.getIcon("/collect/more.png"));
        //添加弹出菜单侦听器
        button.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                popupMenu.removeAll();
                popupMenu.add(addUrlMenuItem());
                popupMenu.addSeparator();
                popupMenu.add(addGroupMenuItem());

                if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                } else {
                    popupMenu.setVisible(false);
                }
            }
        });
        return button;
    }

    private JMenuItem addUrlMenuItem() {
        JMenuItem menuItem = new JMenuItem("添加网页");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CollectUrlPO collectUrlPO = new CollectUrlPO();
                UpdateCollectUrlDialog updateCollectUrlDialog = new UpdateCollectUrlDialog(collectUrlPO, collectBar, false);
                updateCollectUrlDialog.setModal(true);
                updateCollectUrlDialog.setVisible(true);
            }
        });
        return menuItem;
    }

    private JMenuItem addGroupMenuItem() {
        JMenuItem menuItem = new JMenuItem("添加新的分组");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newGroupName = JOptionPane.showInputDialog(null, "请输入新的分组名称");
                CollectGroupService collectGroupService = Solon.context().getBean(CollectGroupService.class);
                if (!StringUtils.isBlank(newGroupName)) {
                    collectGroupService.insert(newGroupName);
                }
                collectBar.refresh();
            }
        });
        return menuItem;
    }


}
